$(document).ready(function() {
	moment.locale("zh-cn");
	window.currentpage = 1;
	ready();
	
});

function ready() {
	queryUserInfo(1);//查询第一页
	//绑定事件
	bind();
	//表单校验
	check();
	showUserName();
}

function initDormLocation() {
	$.ajax({
		url:"/dormms/super/getNoAdminDormLocationDormAdmin",
        type:"post",
        success:function(data){
        	console.log(data);
        	for(var i=0; i<data.length; i++){
        		 $("#insertDormAdminId").append("<option value="+ data[i].dormAdminId+">"+data[i].dormAdminName+"</option>");
        	}
        }
	});
	$.ajax({
		url:"/dormms/super/getAllDormLocation",
        type:"post",
        success:function(data){
        	console.log(data);
        	for(var i=0; i<data.length; i++){
        		$("#insertDormLocationIDByAdmin").append("<option value="+ data[i].dormLocationId+">"+data[i].dormLocationName+"</option>");
        	}
        }
	});
}

function bind() {
	$("#pageSize").bind("change",function(){
		queryUserInfo(window.currentpage);
	});
	$("#searchDormLocationByDormLocationName").bind("click",function(){
		window.currentpage = 1;
		queryUserInfo(window.currentpage);
	});
	$('#addDormLoactionModal').on('hide.bs.modal', function (){
		$("#insertDormLocationId").val("");
		$("#insertDormLocationName").val("");
		$("#insertDormLocationId-error").remove();
		$("#insertDormLocationName-error").remove();
	});
	$('#addDormLoactionAdminModal').on('show.bs.modal', function (){
		initDormLocation();
	});
	$('#addDormLoactionAdminModal').on('hide.bs.modal', function (){
		$("#insertDormLocationIDByAdmin").val("");
		$("#insertDormAdminId").val("");
		$("#insertDormLocationIDByAdmin-error").remove();
		$("#insertDormAdminId-error").remove();
		$("#insertDormLocationIDByAdmin option").remove();
		$("#insertDormAdminId option").remove();
	});
	$('#updateDormLocationAdminInfoModal').on('hide.bs.modal', function (){
		$("#updateDormLocationInfoId").val("");
		$("#updateDormLocationInfoName").val("");
		$("#updateDormLocationInfoId-error").remove();
		$("#updateDormLocationInfoName-error").remove();
	});
	$('#updatePasswordModel').on('hide.bs.modal', function (){
		$("#oldPassword").val("");
		$("#password").val("");
		$("#password_again").val("");
		$("#oldPassword-error").remove();
		$("#password-error").remove();
		$("#password_again-error").remove();
	});
	$('input[name="selectAllDormAdmin"]').bind("click",function(){
		if($(this).is(':checked')){
			$('input[name="selectDormAdmin"]').each(function(){   
                $(this).prop("checked",true);  
            });
		}else{
			$('input[name="selectDormAdmin"]').each(function(){   
                $(this).removeAttr("checked",false);
            });
		}
	});
}

function check(){
	addCheck();
	addAdminCheck();
	updateCheck();
	updatePasswordCheck();
}


function addCheck(){
	//添加校验
	$("#addDormLocation").validate({
		rules:{
			insertDormLocationId:{
				required:true,
				digits:true,
				minlength:6,
				maxlength:6
			},
			insertDormLocationName:{
				required:true,
			},
		},
		messages:{
			insertDormLocationId:{
				required:"请输编号",
				digits:"请输入正确格式的编号",
				minlength:"请输入正确格式的编号",
				maxlength:"请输入正确格式的编号"
			},
			insertDormLocationName:{
				required:"请输入宿舍楼名称",
			},
		},
		submitHandler:function(form){
			var insertDormLocationId = $("#insertDormLocationId").val();
			var insertDormLocationName = $("#insertDormLocationName").val();
			$.ajax({
				type:'post',
				url:'insertDormLocationInfo',
				data:{"dormLocationId":insertDormLocationId,"dormLocationName":insertDormLocationName},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#addDormLoactionModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function addAdminCheck(){
	$("#addDormLocationAdmin").validate({
		rules:{
			insertDormLocationIDByAdmin:{
				required:true,
			},
			insertDormAdminId:{
				required:true,
			},
		},
		messages:{
			insertDormLocationIDByAdmin:{
				required:"请选择宿舍楼",
			},
			insertDormAdminId:{
				required:"请选择管理员",
			},
		},
		submitHandler:function(form){
			var insertDormLocationIDByAdmin = $("#insertDormLocationIDByAdmin").val();
			var insertDormAdminId = $("#insertDormAdminId").val();
			$.ajax({
				type:'post',
				url:'insertDormLocationAdminInfo',
				data:{"dormLocationId":insertDormLocationIDByAdmin,"dormAdminId":insertDormAdminId},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#addDormLoactionAdminModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updateCheck(){
	//修改校验
	$("#updateDormLocation").validate({
		rules:{
			updateDormLocationId:{
				required:true,
				digits:true,
				minlength:6,
				maxlength:6
			},
			updateDormLocationName:{
				required:true,
			},
		},
		messages:{
			updateDormLocationId:{
				required:"请输入学号",
				digits:"请输入正确格式的学号",
				minlength:"请输入正确格式的学号",
				maxlength:"请输入正确格式的学号"
			},
			updateDormLocationName:{
				required:"请输入姓名",
			},
		},
		submitHandler:function(form){
			var dormAdminId = $("#updateDormLocationId").val();
			var dormAdminName = $("#updateDormLocationName").val();
			$.ajax({
				type:'post',
				url:'updateDormLocationInfoBySuper',
				data:{"dormLocationId":dormAdminId,"dormLocationName":dormAdminName},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updateDormLocationAdminInfoModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updatePasswordCheck(){
	$("#updatePassword").validate({
		rules:{
			oldPassword:{
				required:true
			},
			password:{
				required:true
			},
			password_again:{
				required:true,
				equalTo: "#password"
			}
		},
		messages:{
			oldPassword:{
				required:"请输入旧密码"
			},
			password:{
				required:"请输入新密码"
			},
			password_again:{
				required:"请输入新密码",
				equalTo: "两次输入不相同"
			}
		},
		submitHandler:function(form){
			var oldPassword = $("#oldPassword").val();
			var password = $("#password").val();
			$.ajax({
				type:'post',
				url:'updatePassword',
				data:{"oldPassword":oldPassword,"password":password},
				success:function(data){
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updatePasswordModel').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function queryUserInfo(page){
	var currPage = page;
	var pageSize = $("#pageSize").val();
	var dormLocationName = $("#dormLocationName").val();
	$.ajax({
		url:"/dormms/super/queryDormLocationByPageBySuper",
        type:"post",
        data:{"currPage":currPage,"pageSize":pageSize,"dormLocationName":dormLocationName},
        success:function(data){
        	console.log(data);
        	$("#dormLocationInfoList").empty();
        	$("#total").text("共有"+data.total+"条记录");
        	for(var i=0; i<data.list.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td id='findDormAdminId'>"+ data.list[i].dormLocationId +"</td>");
        		 trTemp.append("<td>"+ data.list[i].dormLocationName +"</td>");
        		 trTemp.append("<td>" + "<button type='button' class='btn btn-outline-success btn-sm' onclick='getDormAdminInfoGet(this)'>查看</button>" 
        				 +"<button type='button' class='btn btn-outline-primary btn-sm' onclick='getDormAdminInfoUpdate(this)'>修改</button>"  
        				 + "</td>");
        		 trTemp.appendTo("#dormLocationInfoList");
        	}
        	queryUserRoleByPage(data);
        }
	});
}

function queryUserRoleByPage(data) {
	var options = {
			bootstrapMajorVersion: 3,
			currentPage: data.pageNum,
            totalPages: data.pages,
            numberofPages: 5,
            itemTexts: function(type, page, current) { //修改显示文字
                switch (type) {
                case "first":
                    return "&laquo;";
                case "prev":
                    return "&lt;";
                case "next":
                    return "&gt;";
                case "last":
                    return "&raquo;";
                case "page":
                    return page;
                }
            },
            onPageClicked: function (event, originalEvent, type, page) { //异步换页
            	queryUserInfo(page);
            	window.currentpage=page;
            },
	};
	$('#page').bootstrapPaginator(options); 
}


//获取当前行数据(修改)
function getDormAdminInfoUpdate(obj) {
	var x = $(obj).parent().parent().find("td");
	var dormAdmininfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/super/getAdminDormLocationAllDormAdmin",
		type:"get",
		data:{'dormLocationId':dormAdmininfo},
		success:function(data){
			console.log(data);
			$("#updateDormLocationId").val(data.dormLocation.dormLocationId);
			$("#updateDormLocationName").val(data.dormLocation.dormLocationName);
			$("#updateDormLocationAdminInfoList").empty();
        	for(var i=0; i<data.adminInfos.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td id='findDormAdminId'>"+ data.adminInfos[i].dormAdminId +"</td>");
        		 trTemp.append("<td>"+ data.adminInfos[i].dormAdminName +"</td>");
        		 if(data.adminInfos[i].dormAdminTel!=null){
        			 trTemp.append("<td>"+ data.adminInfos[i].dormAdminTel +"</td>");
        		 }else{
        			 trTemp.append("<td></td>");
        		 }
        		 trTemp.append("<td><button type='button' class='btn btn-outline-danger btn-sm' onclick='deleteDormLocationAdmin(this)'>删除</button></td>");
        		 trTemp.appendTo("#updateDormLocationAdminInfoList");
        	}
			$('#updateDormLocationAdminInfoModal').modal('show');
		}
	});
}
//获取当前行数据(查看)
function getDormAdminInfoGet(obj) {
	var x = $(obj).parent().parent().find("td");
	var dormAdmininfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/super/getAdminDormLocationAllDormAdmin",
		type:"get",
		data:{'dormLocationId':dormAdmininfo},
		success:function(data){
			console.log(data);
			$("#getDormLocationAdminInfoId").val(data.dormLocation.dormLocationId);
			$("#getDormLocationAdminInfoName").val(data.dormLocation.dormLocationName);
			$("#getDormLocationAdminInfoList").empty();
        	for(var i=0; i<data.adminInfos.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td id='findDormAdminId'>"+ data.adminInfos[i].dormAdminId +"</td>");
        		 trTemp.append("<td>"+ data.adminInfos[i].dormAdminName +"</td>");
        		 if(data.adminInfos[i].dormAdminTel!=null){
        			 trTemp.append("<td>"+ data.adminInfos[i].dormAdminTel +"</td>");
        		 }else{
        			 trTemp.append("<td></td>");
        		 }
        		 //trTemp.append("<td><button type='button' class='btn btn-outline-danger btn-sm'>删除</button></td>");
        		 trTemp.appendTo("#getDormLocationAdminInfoList");
        	}
			$('#getDormLocationAdminInfoModal').modal('show');
		}
	});
}

function deleteDormLocationAdmin(obj){
	var x = $(obj).parent().parent().find("td");
	var dormAdminId = x.eq(0).text();
	$.ajax({
		url:"deleteDormLocationAdmin",
		type:"get",
		data:{'dormAdminId':dormAdminId},
		success:function(data){
			var dormLocationId = $("#updateDormLocationId").val();
			$.ajax({
				url:"/dormms/super/getAdminDormLocationAllDormAdmin",
				type:"get",
				data:{'dormLocationId':dormLocationId},
				success:function(data){
					console.log(data);
					$("#updateDormLocationId").val(data.dormLocation.dormLocationId);
					$("#updateDormLocationName").val(data.dormLocation.dormLocationName);
					$("#updateDormLocationAdminInfoList").empty();
		        	for(var i=0; i<data.adminInfos.length; i++){
		        		 var trTemp = $("<tr></tr>");
		        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
		        		 trTemp.append("<td id='findDormAdminId'>"+ data.adminInfos[i].dormAdminId +"</td>");
		        		 trTemp.append("<td>"+ data.adminInfos[i].dormAdminName +"</td>");
		        		 if(data.adminInfos[i].dormAdminTel!=null){
		        			 trTemp.append("<td>"+ data.adminInfos[i].dormAdminTel +"</td>");
		        		 }else{
		        			 trTemp.append("<td></td>");
		        		 }
		        		 trTemp.append("<td><button type='button' class='btn btn-outline-danger btn-sm' onclick='deleteDormLocationAdmin(this)'>删除</button></td>");
		        		 trTemp.appendTo("#updateDormLocationAdminInfoList");
		        	}
					$('#updateDormLocationAdminInfoModal').modal('show');
				}
			});
		}
	});
}


function showPasswordModel() {
	$('#updatePasswordModel').modal('show');
}

function showUserName() {
	$.ajax({
		url:"getSuperInfo",
		type:"get",
		success:function(data){
			$("#userId").text(data.userId);
		}
	});
}
